//
//  ZTX YiChangChuLiListController.m
//  NtoccDriverNew
//
//  Created by 张天兴 on 2023/11/8.
//  Copyright © 2023 GCY. All rights reserved.
//

#import "ZTXYiChangChuLiListController.h"
#import "ZTXYiChangChliCell.h"
#import "ZTXYiChangChuLiController.h"
#import "ZTXYiChangChuLiAlControllerViewController.h"

@interface ZTXYiChangChuLiListController ()<UITableViewDataSource, UITableViewDelegate>

@property (strong , nonatomic)UITableView *tableView;

@property (strong , nonatomic)NSMutableArray *dataArray;

@property (strong , nonatomic)UIImageView *foundImageView;

@property (nonatomic, assign) NSInteger pageNum;

@end
static NSString *const cellID = @"ZTXYiChangChliCell";

static const CGFloat cellHeight = 120;
@implementation ZTXYiChangChuLiListController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _pageNum = 1;
    
    [self setupBaseView];
    
    [self freightIndexListRequest];
}

- (void)setupBaseView {
    
    self.navigationItem.title = @"异常处理";
    
    self.view.backgroundColor = BackgroundColor;
    
    [self.view addSubview:self.tableView];
}

- (void)freightIndexListRequest {
    
    WEAKSELF
    if (_pageNum == 1) {
        [self.dataArray removeAllObjects];
    }
    
    NSDictionary *params = @{
        @"pageNum": [NSNumber numberWithInteger:_pageNum],
        @"pageSize": DEFULT_LOAD_NUM,
    };
     NSString *urlString = CJStringWithFormat(@"%@%@/%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], TRANSPORT_ABNORMAL,self.orderCode);
     [HTTPManager getNetworkRequest:urlString withParameter:params withVC:self netName:@"异常处理反馈信息" success:^(id response, NSString *msg) {
         if ([response count] != 0) {
             NSArray *arr = response;
             for (NSDictionary *dic in arr) {
                 [weakSelf.dataArray addObject:dic];
             }
//             [weakSelf.tableView.mj_header endRefreshing];
//             [weakSelf.tableView.mj_footer endRefreshing];

         }else{
             if (_pageNum == 1) {
                 weakSelf.foundImageView.hidden = NO;
                 weakSelf.tableView.hidden = YES;
             }
//             [weakSelf.tableView.mj_header endRefreshing];
//             [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];

         }

         [weakSelf.tableView reloadData];
     } failure:^(NSInteger code, NSString *msg) {
         if(code == 500){
             [self.navigationController popViewControllerAnimated:YES];
         }
     }];
}

//上拉刷新数据
- (void)loadNewData{
    _pageNum = 1;
    [self freightIndexListRequest];
}

- (void)loadMoreData{
    _pageNum ++;
    [self freightIndexListRequest];
}


#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *dic = self.dataArray[indexPath.row];
    ZTXYiChangChliCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    cell.timeLabel.text = CJStringWithFormat(@"%@",dic[@"happenTime"]);
   
    if([CJStringWithFormat(@"%@",dic[@"description"]) isNotBlank]){
        cell.questionLabel.text = CJStringWithFormat(@"问题描述: %@",dic[@"description"]);
    }else{
        cell.questionLabel.text = @"问题描述:";
    }
    if(![DCSpeedy isBlankString:CJStringWithFormat(@"%@",dic[@"handleIdea"]])){
        cell.chuliLabel.text = CJStringWithFormat(@"处理描述: %@",dic[@"handleIdea"]);
    }else{
        cell.chuliLabel.text = @"处理描述:";
    }
    cell.addressLabel.text = CJStringWithFormat(@"地        址: %@",dic[@"address"]);
    if([dic[@"isHandle"] intValue] == 0){
        cell.stateLabel.textColor = [UIColor redColor];
        cell.stateLabel.text = @"未处理";
    }else{
        cell.stateLabel.textColor = [UIColor greenColor];
        cell.stateLabel.text = @"已处理";
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return cellHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataArray[indexPath.row];
    ZTXYiChangChuLiAlControllerViewController *tshzvc = [[ZTXYiChangChuLiAlControllerViewController alloc] init];
     tshzvc.orderCode = dic[@"code"];
    tshzvc.type = @"0";
     [self.navigationController pushViewController:tshzvc animated:YES];
}


#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 10, ScreenW, ScreenH - NavHeight - MenuBarHeight - 10);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        
        //注册单元格
        [_tableView registerNib:[UINib nibWithNibName:@"ZTXYiChangChliCell" bundle:nil] forCellReuseIdentifier:cellID];
//        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
//        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    
    return _tableView;
}

- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
- (UIImageView *)foundImageView {
    if (!_foundImageView) {
        _foundImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 170, 170)];
        _foundImageView.image = image(@"gcy_img_list_found");
        _foundImageView.contentMode = UIViewContentModeScaleAspectFit;
        _foundImageView.cy_centerX = self.view.cy_centerX;
        _foundImageView.cy_centerY = self.view.cy_centerY;
        [self.view addSubview:_foundImageView];
    }
    return _foundImageView;
}


@end
